<?php 

class Lemar_Menu
{
	
//-------------------------------------------------------------------------------------------------

	public static function saveMenuItem($oRequest, $image = null)
	{
		$idRestaurant		= $oRequest->getParam('id_restaurant');
		$itemName		 	= $oRequest->getParam('name');
		$itemDescription 	= $oRequest->getParam('description');
		
		$idItemGroup		= $oRequest->getParam('item_group');
		if('Add new' == $idItemGroup)
		{
			$nameItemGroup		= $oRequest->getParam('new_group_name');
			$idItemGroup	= models_MenugroupsMapper::save(array('name' => $nameItemGroup));
		}
		$idItemName			= models_MenuitemsMapper::saveArray(array(
													'restaurant_id'	=> $idRestaurant,
													'group_id'		=> $idItemGroup,
													'name'			=> $itemName,
													'descriptions'	=> $itemDescription,
													'status'		=> 'active',
													'image'			=> $image,
		), 'models_DbTable_Menuitems'); 
		
		return $idItemName;
	}		

//-------------------------------------------------------------------------------------------------

	public static function saveSingleMenuPrices($oRequest, $idItemName)
	{
		$itemPrice			= $oRequest->getParam('single_price');
		$idPriceGroupName	= models_PricegroupsMapper::save(array('name' => 'single'));
		
		$ret = models_MenupricesMapper::save(array(
									'menu_item_id'		=> $idItemName,
									'price_group_id'	=> $idPriceGroupName,
									'name'				=> null,
									'price'				=> $itemPrice,
			));
		
		return $ret;	
	}
		
//-------------------------------------------------------------------------------------------------

	public static function saveGroupMenuPrices($oRequest, $idItemName)
	{
		$priceGroupName  	= $oRequest->getParam('price_group_name');
		$priceOptionsName	= $oRequest->getParam('price_options_name');
		$priceOptionsPrice	= $oRequest->getParam('price_options_price');
//			phpinfo(); die;
		foreach ($priceGroupName as $id => $name)
		{
			$idPriceGroupName 		= models_PricegroupsMapper::save(array('name' => $name));
			$aIdPriceGroupName[$id]	= $idPriceGroupName;
		}
		foreach ($priceOptionsName as $id => $aOption)
		{
			foreach ($aOption as $optId => $optName)
			{
				if ($optName)
					$aPriceOptions[$id][$optId]['name'] = $optName;
			}
		}
		foreach ($priceOptionsPrice as $id => $aOption)
		{
			foreach ($aOption as $optId => $optPrice)
			{
				if ($optPrice)
					$aPriceOptions[$id][$optId]['price'] = $optPrice;
			}
		}
		
		foreach ($aPriceOptions as $idPriceGroupName => $aVal)
		{
			foreach ($aVal as $idOption => $aOption)
			{
				models_MenupricesMapper::save(array(
										'menu_item_id'		=> $idItemName,
										'price_group_id'	=> $idPriceGroupName,
										'name'				=> $aOption['name'],
										'price'				=> $aOption['price'],
				));
			}
		}
		
	}
	
//-------------------------------------------------------------------------------------------------

	public static function saveOptionsGroupName($oRequest, $idItemName)
	{
		$optionsGroupName	 = $oRequest->getParam('options_group_name');
		$optionsOptionsName	 = $oRequest->getParam('options_options_name');
		$optionsOptionsPrice = $oRequest->getParam('options_options_price');
		$aOptionConditions	 = $oRequest->getParam('options_group_condition');
		$aOptionConditionsMore	 = $oRequest->getParam('options_group_condition_more');
		foreach ($optionsGroupName as $id => $name)
		{
			$optionCondition			= $aOptionConditions[$id];
			if (isset($aOptionConditionsMore[$id]))
			{
				$optionCondition		= $optionCondition . '+';
			}
			if (! isset($optionCondition))
			{
				$optionCondition = '0+';
			}
			$idOptionsGroupName 		= models_OptiongroupsMapper::save(array('name' => $name, 
																				'condition' => $optionCondition));
			$aIdOptionsGroupName[$id]	= $idOptionsGroupName;
		}
		foreach ($optionsOptionsName as $id => $aOption)
		{
			foreach ($aOption as $optId => $optName)
			{
				if ($optName)
					$aOptionsOptions[$id][$optId]['name'] = $optName;
			}
		}
		foreach ($optionsOptionsPrice as $id => $aOption)
		{
			foreach ($aOption as $optId => $optPrice)
			{
				if ($optPrice)
				{
					$aOptionsOptions[$id][$optId]['price'] = $optPrice;
				}
				else 
				{
					$aOptionsOptions[$id][$optId]['price'] = '0+';
				}
			}
		}
		foreach ($aOptionsOptions as $idOptionsGroupName => $aVal)
		{
			foreach ($aVal as $idOption => $aOption)
			{
				print_r($aOption);
				@models_MenuoptionsMapper::save(array(
										'menu_item_id'		=> $idItemName,
										'option_group_id'	=> $aIdOptionsGroupName[$idOptionsGroupName],
										'name'				=> $aOption['name'],
										'price'				=> $aOption['price'],
				));
			}
		}
	}
	
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
	
}